﻿using System;
using System.DHTML;
using Sys;
using Sys.UI;
using Sys.Mvc;

using JQuerySharp;
using VietPublisher.Scripts.Resources;

static public class SC_Department
{
    public static string ControllerUrl = "Department";


    #region 부서관리

    public static void SearchDeptList(Dictionary form)
    {
        UIHelper.SubmitAjax(form, "Post", "divEmpMstList", null, null);

    }
    
    /// <summary>
    /// Save
    /// </summary>
    /// <param name="anchor"></param>
    public static void SaveDeptMst(DOMElement anchor)
    {
        // validate parent code
        TableRowElement curRow = (TableRowElement)anchor.ParentNode.ParentNode;
        SelectElement select = (SelectElement)curRow.Cells[3].FirstChild;

        string deptKey = select.Attributes.GetNamedItem("deptkey").Value;
        string pDeptKey = select.Value;

        if (deptKey == pDeptKey)
        {
            Script.Alert("부모부서가 다시 설정 해 주세요.");
            return;
        }

        InputElement preEle = (InputElement)curRow.Cells[4].FirstChild;
        string preDeptCode = preEle.Value;

        string captainCode = Utils.GetInputValue("CaptainCode_" + curRow.Cells[0].InnerText);

        string href = SC_Common.BasePath + ControllerUrl + string.Format("/SaveDeptMst?deptKey={0}&pdeptkey={1}&predeptcode={2}&captainCode={3}",
            deptKey, pDeptKey, preDeptCode, captainCode);

        UIHelper.LoadAjax(href, null, new MvcCallback(delegate(AjaxContext context)
            {
                Script.Alert(Common.OperationSuccess);
                Utils.ReloadWindow();
            }), null);
    }

    public static string CaptainCodeKey = "";
    public static string CaptainNameKey = "";
    public static void OpenSelectCaptainCode(TableRowElement row)
    {
        InputElement ele = (InputElement)row.GetElementsByTagName("input")[0];
        CaptainNameKey = ele.ID;

        ele = (InputElement)row.GetElementsByTagName("input")[1];
        CaptainCodeKey = ele.ID;

        SC_Popup.OnSelectUserEmp = OnReturnEmpCode;
        SC_Popup.OpenEmpDeptPopup("");
    }


    /// <summary>
    /// Return Captain Code
    /// </summary>
    /// <param name="userId"></param>
    /// <param name="userName"></param>
    /// <param name="empCode"></param>
    /// <param name="empName"></param>
    /// <param name="deptCode"></param>
    /// <param name="deptName"></param>
    /// <param name="email"></param>
    /// <param name="pstName"></param>
    public static void OnReturnEmpCode(string userId, string userName, string empCode, string empName, string deptCode, string deptName, string email, string pstName)
    {
        Utils.SetInputValue(CaptainCodeKey, empCode);
        Utils.SetInputValue(CaptainNameKey, empName);
    }

    public static void SyncDeptsWithAD()
    {
        //if (Script.Confirm(Common.SyncUsersWithADMsg))
        //{
        //    string href = SC_Common.BasePath + ControllerUrl + "/SyncDeptsWithAD";
        //    UIHelper.LoadAjax(href, null, new MvcCallback(delegate(AjaxContext context)
        //        {
        //            if (context.request.Executor.ResponseData == "")
        //            {
        //                Script.Alert(Common.SyncDeptWithADCompleted);
        //            }
        //            else
        //            {
        //                Script.Alert(context.request.Executor.ResponseData);
        //            }
                    
        //        }), null);
        //}
    }
    
    #endregion
}
